/**
 * 
 */
package model.container;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

import model.Cursist;
import model.HerscholingCursist;
import model.VrijeCursist;
import persistenty.ContainerTekstPersisteerbaar;

/**
 * This class is intended to setup the Cursisten container object.
 * 
 * @author Nicolas Vanderhulst
 * @version 20111009
 * 
 */
public class Cursisten extends ContainerTekstPersisteerbaar implements Comparable<Cursisten>, Iterable<Cursist>,
		Cloneable {
	public HashSet<Cursist> cursisten;
	private int cursistID = 0;

	/**
	 * @return the cursistID
	 */
	public int getCursistID() {
		return cursistID;
	}

	/**
	 * @param cursistID
	 *            the cursistID to set
	 */
	public void setCursistID(int cursistID) {
		this.cursistID = cursistID;
	}

	public Cursisten() throws CursistenException {
		cursisten = new HashSet<Cursist>();
		persistentyFiles = new ArrayList<String>();
		persistentyFiles.add("src/persitentyfiles/HerscholingCursist.txt");
		persistentyFiles.add("src/persitentyfiles/VrijeCursist.txt");
	}

	public void add(Cursist c) throws CloneNotSupportedException, CursistenException {
		if (!cursisten.add(c)) {
			throw new CursistenException("Deze cursist bestaat reeds");
		}
	}

	public void remove(Cursist c) throws IOException {
		cursisten.remove(c);
	}

	public boolean checkCursistCodeExists(Cursist a) throws CloneNotSupportedException {
		boolean b = false;
		for (Cursist c : cursisten) {
			if (c.getCursistNummer() == a.getCursistNummer()) {
				b = true;
			}
		}
		return b;
	}

	@SuppressWarnings("boxing")
	public String[] getCursisten() {
		int i = 0;
		String[] cursistenAlsString = new String[cursisten.size()];
		Iterator<Cursist> it = cursisten.iterator();
		while (it.hasNext()) {
			Cursist c = it.next();
			String cursistType = null;
			if (c instanceof VrijeCursist) {
				cursistType = "Vrije Cursist";
			} else if (c instanceof HerscholingCursist) {
				cursistType = "Herscholing Cursist";
			}
			cursistenAlsString[i] = String.format("%s   %s   %s", c.getID(), c.getNaam(), cursistType);
			i++;
		}
		return cursistenAlsString;

	}

	@Override
	public Iterator<Cursist> iterator() {
		return cursisten.iterator();
	}

	@Override
	public int compareTo(Cursisten arg0) {
		return this.toString().compareTo(arg0.toString());
	}

	@Override
	public void schrijven() throws IOException {
		try {
			BufferedWriter writer = null;
			for (Cursist c : cursisten) {
				writer = new BufferedWriter(new FileWriter(String.format("src/persitentyfiles/%s.txt", c.getClass()
						.getSimpleName()), false));
				writer.write(c.maakTekstVanObject());
			}
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((cursisten == null) ? 0 : cursisten.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Cursisten)) {
			return false;
		}
		Cursisten other = (Cursisten) obj;
		if (cursisten == null) {
			if (other.cursisten != null) {
				return false;
			}
		} else if (!cursisten.equals(other.cursisten)) {
			return false;
		}
		return true;
	}

	/**
	 * @param cursisten
	 *            the cursisten to set
	 */
	public void setCursisten(HashSet<Cursist> cursisten) {
		this.cursisten = cursisten;
	}

	/**
	 * Geeft een Cursist terug volgens zijn code
	 */
	public Cursist getCursistByCode(int cursistCode) {
		Cursist tempCursist = null;
		for (Cursist c : this.cursisten) {
			if (c.getCursistNummer() == cursistCode) {
				tempCursist = c;
			}
		}
		return tempCursist;
	}

	public int getHoogsteID() {
		int cursistNummer = -1;
		for (Cursist c : cursisten) {
			if (c.getID() > cursistNummer) {
				cursistNummer = c.getID();
			}
		}
		return cursistNummer;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Cursisten [cursisten=" + cursisten + "]";
	}

	/**
	 * @return
	 * @see java.util.HashSet#clone()
	 */
	@Override
	public Object clone() {
		return cursisten.clone();
	}

	@Override
	public void createObject() {
		try {
			Cursist cursist = null;
			if (inputFile.equalsIgnoreCase("src/persitentyfiles/VrijeCursist.txt")) {
				cursist = new VrijeCursist(cursistID, inputFile, inputFile);
			} else if (inputFile.equalsIgnoreCase("src/persitentyfiles/HerscholingCursist.txt")) {
				cursist = new HerscholingCursist(cursistID, inputFile, inputFile, inputFile);
			}
			if (cursist != null) {
				cursist = cursist.maakObjectVanTekst(objectAlsTekst);
				cursisten.add(cursist);
				cursistID = (cursistID < cursist.getID() ? cursist.getID() : cursistID);// Update
																						// hoogste
																						// huidige
																						// evenementid
			}
		} catch (CursistenException evenementException) {
			evenementException.printStackTrace();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
